﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BDSimpleStatus.Models
{
    public class ServerCheckFormViewModel
    {

        public ServerCheckModel scm { get; set; }
        public string rowcssclass { get; set; }
        public string CheckHttpCss { get; set; }
        public string CheckPingCss { get; set; }


        public ServerCheckFormViewModel(ServerCheckModel model)
        {
            scm = model;
            SetCheckIsOkCssClass();
            SetRowCssClass();

        }

        // Determines the css class names needed for the row
        private void SetRowCssClass()
        {
            // scm.CheckHttp, scm.CheckPing, scm.IsHttpOk, scm.IsPingOk, scm.IsEnabled
            bool PingUp = false;
            bool HttpUp = false;
            if (scm.IsEnabled && (scm.CheckHttp || scm.CheckPing))
            {
                if (scm.CheckHttp)
                    if (scm.IsHttpOk)
                        HttpUp = true;
                    else
                        HttpUp = false;
                else
                    HttpUp = true;  // Because are actually not doing a test

                if (scm.CheckPing)
                    if (scm.IsPingOk)
                        PingUp = true;
                    else
                        PingUp = false;
                else
                    PingUp = true;
                if (HttpUp && PingUp)
                    rowcssclass = "serverup";
                else
                    rowcssclass = "serverdown";

            }
            else
            {
                rowcssclass = "checkdisabled";
                CheckHttpCss = "checkisokdisabled";
                CheckPingCss = "checkisokdisabled";
            }
        }

        // Sets a css class for enabled or disabled checks
        private void SetCheckIsOkCssClass()
        {
            if (scm.CheckHttp)
            {
                CheckHttpCss = "checkisokenabled";
                if (!scm.IsHttpOk)
                    CheckHttpCss += " checkfailed";
            }
            else
                CheckHttpCss = "checkisokdisabled";

            if (scm.CheckPing)
            {
                CheckPingCss = "checkisokenabled";
                if (!scm.IsPingOk)
                    CheckPingCss += " checkfailed";
            }
            else
                CheckPingCss = "checkisokdisabled";

        }
    }
}